package com.obsidian.free.slideshow;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class SlideshowConfigureImagesOptions extends ListActivity implements OnItemClickListener {
   static public final int ACTION_DELETE = 0;
   static public final int ACTION_NEXT = 1;
   static public final int ACTION_PREV = 2;
   
   int mPosition;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
      mPosition = getIntent().getIntExtra("position", -1);
      if(mPosition < 0) {
         finish();
      }
      
      StringAdapter adapter = new StringAdapter(this);
      setListAdapter(adapter);
      getListView().setOnItemClickListener(this);
   }
   
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
      Intent intent = new Intent();
      intent.putExtra("position", mPosition);
      
      int result = RESULT_OK;
      switch(position) {
         case 0:
            // delete
            intent.putExtra("action", ACTION_DELETE);
            break;
         case 1:
            // move next
            intent.putExtra("action", ACTION_NEXT);
            break;
         case 2:
            // move prev
            intent.putExtra("action", ACTION_PREV);
            break;
         default:
            result = RESULT_CANCELED;
      }
      setResult(result, intent);
      finish();
   }
   
   private class StringAdapter extends BaseAdapter {
      String[] items = {
         getString(R.string.config_images_options_delete),
         getString(R.string.config_images_options_next),
         getString(R.string.config_images_options_prev)
      };
      
      Context mContext;
      LayoutInflater mInflate;
      public StringAdapter(Context context) {
         mContext = context;
         mInflate = LayoutInflater.from(context);
      }
      public int getCount() {
         return items.length;
      }
      public Object getItem(int position) {
         return items[position];
      }
      public long getItemId(int position) {
         return 0;
      }
      public View getView(int position, View convertView, ViewGroup parent) {
         TextView view = (TextView) mInflate.inflate(android.R.layout.simple_list_item_1, null);
         view.setText(items[position]);
         view.setTag(view);
         
         return view;
      }
   }
}
